package com.lwj.liwanjia.adapter;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.KdBean;
import com.lwj.liwanjia.utils.MyGridView;
import com.superrecycleview.superlibrary.adapter.BaseViewHolder;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/9/15.
 */

public class RecordAdapter extends SuperBaseAdapter<KdBean.DataBean> {
    private Context mContext;

    public RecordAdapter(Context context, List<KdBean.DataBean> data) {
        super(context, data);
        this.mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder holder, KdBean.DataBean item, int position) {
        ImageView imageView = holder.getView(R.id.iv_state);
        if (item.getStatus().equals("已完成")) {
            holder.setText(R.id.tv_state, "我的已收件");
            imageView.setImageResource(R.mipmap.yisongda);
        } else if (item.getStatus().equals("待收货")) {
            holder.setText(R.id.tv_state, "我的待收件");
            imageView.setImageResource(R.mipmap.peisongzhong);
        }
        holder.setText(R.id.tv_bianhao, "订单号:" + item.getTradeId());
        MyGridView myGridView = holder.getView(R.id.grid_image);
        RecordImageAdapter imageAdapter = new RecordImageAdapter(mContext, item.getCommodity());
        myGridView.setAdapter(imageAdapter);
        TextView tvNumber = holder.getView(R.id.tv_number);
        tvNumber.setText(setColor("共" + item.getAmount() + "件商品"));
        holder.setText(R.id.tv_price, "￥" + item.getTotalpay());
    }

    @Override
    protected int getItemViewLayoutId(int position, KdBean.DataBean item) {
        return R.layout.record_item;
    }

    private SpannableStringBuilder setColor(String str) {
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        style.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.colorEC921A)), 1, str.lastIndexOf("件"), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return style;
    }
}
